package com.longrise.android.byjk.plugins.request;

import android.content.Context;
import android.os.Bundle;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.common.datasource.remote.LoadDataManager;

/**
 * Created by sujizhong on 2016/12/20.
 * <p>
 * RequestThread需要AppBaseRequest子类和AppBaseResult子类
 * AppBaseRequest子类处理封装请求数据
 * AppBaseResult子类处理解析返回数据
 * <p>
 * 请求方法request()
 * 如果请求的数据结构中含有附加参数,需要传递使用request(Bundle extraParams)
 * 在相关的AppBaseRequest子类getRequestParams()方法中做解析
 * <p>
 * <p>
 * 例子：
 * ChangeInforRequest changeInforRequest = new ChangeInforRequest(new ChangeInforResult());
 * changeInforRequest.setApi(api);
 * RequestThread requestThread = new RequestThread(changeInforRequest);
 * requestThread.request(this);
 */

public class RequestThread {

    private static final String TAG = "RequestThread";

    private AppBaseRequest mRequest = null;

    public RequestThread() {
    }

    public RequestThread(AppBaseRequest request) {
        setRequest(request);
    }

    public void setRequest(AppBaseRequest request) {
        this.mRequest = request;
    }

    /**
     * 默认请求
     */
    public void request(Context cxt) {
        String api = mRequest.getApi();
        Object requestObjext = mRequest.getRequestParams();
        commit(cxt, api, requestObjext);
    }

    /**
     * 请求中存在附加参数（需要从Activity中传递时）
     */
    public void request(Context cxt, Bundle extraParams) {
        String api = mRequest.getApi();
        Object requestObjext = mRequest.getRequestParams(extraParams);
        commit(cxt, api, requestObjext);
    }

    /**
     * 请求中，如果无法从API区分不同接口时
     */
    public void request(Context cxt, int tag, Bundle extraParams) {
        String api = mRequest.getApi();
        Object requestObject = mRequest.getRequestParams(tag, extraParams);
        commit(cxt, api, requestObject);
    }

    private void commit(Context cxt, String api, Object reqestParams) {
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, api, (EntityBean) reqestParams, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    if (mRequest.getResult() != null) {
                        mRequest.getResult().onSuccess(result);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                if (mRequest.getResult() != null) {
                    mRequest.getResult().onFinish();
                }
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                if (mRequest.getResult() != null) {
                    mRequest.getResult().onFailed(key);
                }
            }
        });
    }
}
